package com.wixc.controller;

import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wixc.entity.UserBean;
import com.wixc.service.UserService;


@Controller
@RequestMapping(value="/")
public class UserController {

	
	@Autowired
	private UserService userServiceimpl;
	
	static int total=0;
	
	@RequestMapping(value = "/json/jiexi", method = RequestMethod.GET)
	public ModelAndView jxJson() {
		ModelAndView mv = new ModelAndView();
		return mv;
	}
	
	/**
	 * 增dsadds
	 * @param userBean
	 */
	@RequestMapping(value = "/save")
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,UserBean userBean) {

		
		userServiceimpl.save(userBean);
		return null;
	}
	/**
	 * 删
	 * @param jsonBean
	 */
	@RequestMapping(value = "/delete")
	public ModelAndView delete(int id,HttpServletRequest request,HttpServletResponse response) {
		
		userServiceimpl.delete(id);
		return null;
	}
	/**
	 * 查询 一条  根据id
	 */
	@RequestMapping(value = "/getBeanById/{id}")
	public ModelAndView getBeanById( @PathVariable String id,HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("view/list_detail");
		UserBean bean = this.userServiceimpl.getBeanById(Integer.parseInt(id.replaceAll("[^\\d]", "")));
		mv.addObject("bean",bean);
		return mv;
	}	
	/**
	 * 修改
	 * @param jsonBean
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public ModelAndView update(UserBean userBean,HttpServletRequest request) {
		userServiceimpl.update(userBean);
		return null;
	}
	
	public int select() {
		List<UserBean> list = userServiceimpl.select();
		return list.size();
	}
	/**
	 * 查询selectByLimit
	 * @param jsonBean
	 */
	@RequestMapping(value = "/zhihu/{page}")
	public ModelAndView selectByLimit(@PathVariable int page,HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		List<UserBean> lists = userServiceimpl.selectByLimit(page*15, 15);
		mv.addObject("list",lists);
		mv.addObject("page",0);
		mv.setViewName("view//list");
		return mv;
	}
	
	
	/**
	 * 查询selectByLimitAjax
	 * @param jsonBean
	 */
	@RequestMapping(value = "/selectByLimitAjax", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> selectByLimitAjax( int page,HttpServletRequest request,HttpServletResponse response) {
		List<UserBean> lists = userServiceimpl.selectByLimit(page*15, 15);
		Map<String, Object> map=new LinkedHashMap<String, Object>();
		map.put("nextpage", page+1);
		map.put("data", lists);
		return map;
	}
}
